﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model;
using Cn.Edwon.Web;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz;
using System.Reflection;
using Cn.Cellula.Model.System;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Content.Category {
    public partial class Edit : PageSupport {
        String Key = null;
        String ParentKey = null;
        String TypeKey = null;
        ICategoryBiz biz = BizFactory.Get<ICategoryBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            Key = Request.QueryString["id"];
            ParentKey = Request.QueryString["pid"];
            TypeKey = Request.QueryString["tid"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(Key)) {
                    FillForm(Key);
                } else {
                    if (!String.IsNullOrEmpty(ParentKey))
                        this.ddl_Parent.SelectedValue = ParentKey;
                }
            }
        }

        private void InitForm() {
            ClientBiz.BindCodeList(ref ddl_CategoryType, CodeTypeDef.Category.Key, true, TypeKey);

            //try {
            //    TSysCodeValueEO ceo = biz.Load<TSysCodeValueEO>(TypeKey);
            //    this.txt_TypeName.Text = ceo.CodeName;
            //} catch (Exception ex) {
            //    log.Error(ex.Message);
            //    this.txt_Name.Text = "未指定有效的分类类型";
            //}

            this.ddl_Parent.DataSource = biz.LoadCatalog2ListItem(this.OwnerKey, TypeKey, "..", Key, false);
            this.ddl_Parent.DataValueField = "Value";
            this.ddl_Parent.DataTextField = "Text";
            this.ddl_Parent.DataBind();
        }

        private void FillForm(String key) {
            TSysCategoryEO eo = biz.Load<TSysCategoryEO>(key,this.OwnerKey);
            if (eo != null) {
                this.txt_Alias.Text = eo.CategoryAlias;
                this.txt_Name.Text = eo.CategoryName;
                this.ddl_Parent.SelectedValue = eo.ParentId;
                this.txt_OrderNubmer.Text = eo.OrderNumber + "";
                this.txt_Url.Text = eo.Url;
                this.txt_Desc.Text = eo.Description;
                try {
                    this.ddl_CategoryType.SelectedValue = TypeKey;
                } catch { }
            }
        }

        private void BindData2EO(ref TSysCategoryEO eo) {
            eo.CategoryAlias = this.txt_Alias.Text;
            eo.CategoryName = this.txt_Name.Text;
            eo.ParentId = this.ddl_Parent.SelectedValue;
            try {
                eo.OrderNumber = Convert.ToInt32(this.txt_OrderNubmer.Text);
            } catch {
                eo.OrderNumber = 0;
            }
            eo.Url = this.txt_Url.Text;
            eo.Description = this.txt_Desc.Text;
            eo.TypeKey = this.ddl_CategoryType.SelectedValue;
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            TSysCategoryEO eo = new TSysCategoryEO();
            if (String.IsNullOrEmpty(Key)) {
                BindData2EO(ref eo);
                TSysCategoryEO _eo = biz.LoadByName(eo.CategoryName, TypeKey, this.OwnerKey);
                if (_eo != null) {
                    SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.Category.NameExist);
                    return;
                } else {
                    eo.OwnerKey = this.OwnerKey;
                    biz.Save<TSysCategoryEO>(eo);
                    BizFactory.Get<ILogBiz>().LogInsert("分类", eo.CategoryName, this.Actor, Request);
                }
            } else {//update
                eo = biz.Load<TSysCategoryEO>(Key,this.OwnerKey);
                BindData2EO(ref eo);
                biz.Update<TSysCategoryEO>(eo);
                BizFactory.Get<ILogBiz>().LogUpdate("分类",eo.CategoryName, this.Actor, Request);
            }
            Response.Redirect("List.aspx?tid=" + eo.TypeKey + "&t=" + Key);
            //SiteInfo.SendSucceed(Page, "List.aspx?tid="+TypeKey+"&t=" + Key);
        }
    }
}
